<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/26 0026
 * Time: 14:41
 */

namespace app\api\controller;


use app\admin\model\Productspecgroups;
use app\admin\model\Producttype;
use app\admin\model\Storeproduct;
use app\common\model\Groupproduct;
use think\Db;

/**
 * Class Product
 * @package app\api\controller
 * 商品
 * 丁应清
 */
class Product extends BaseController
{

    public function getProductPageSite()
    {
        $param = self::getHttpParam();
        if (empty($param->tenantId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'tenantId不能这空');
        } else {
            $tenantId = $param->tenantId;

            $setting = db('wxcommon')->where(['tenantId' => $tenantId])->find();
            if(!$setting){
                $setting = [
                    'categoryNavType' => 1,
                    'isShowBanner' => 2,
                    'isShowCoupon' => 2,
                    'isShowStore' => 1,
                ];
            }

            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $setting);
        }
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 查找商品类型
     */
    public function getProductClass()
    {
        $param = self::getHttpParam();
        if (empty($param->tenantId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'tenantId不能这空');
        } else {
            $tenantId = $param->tenantId;
            $productType = Producttype::where('tenantId', $tenantId)->order('orderNo', 'asc')->select();
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $productType);
        }
    }

    /**
     * @return \think\response\Json
     * 商品列表
     */
    public function getProductList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;
        if (empty($param->storeId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'storeId不能这空');
        } else if (empty($param->typeId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'typeId不能这空');
        } else {
            if (!empty($param->start)) {
                $start = $param->start;
            }
            if (!empty($param->length)) {
                $length = $param->length;
            }

            $storeId = $param->storeId;
            $typeId = $param->typeId;
            $products = Storeproduct::alias('a')->join('product b', 'a.product_id=b.id')->where('store_id', $storeId)->where('typeId',$typeId)->select();
            return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $products);
        }
    }

    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 商品详情
     */
    public function getProductDetails()
    {
        $param = self::getHttpParam();
        if (empty($param->productId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'productId不能这空');
        } else {

            $productModel = new \app\admin\model\Product();

            $product = $productModel->getProduct($param);

            if($product->isIntegralPurchase == $productModel::IS_INTEGALPURCHASE_2){
                $product->getIntegralProduct;
            }

            if (empty($product)) {
                return show(config('status.EMPTY_STATUS'), self::NOT_DATA, '未查到相关商品');
            } else {
                $images = Db::table('productdtlimg')->where(['productId' => $product->id])->field('imgUrl')->select();
                $product->images = array_column($images, 'imgUrl');

                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $product);
            }
        }
    }

    /**
     * @param $data
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 查询团购商品信息
     */
    public function getGroupLimitCount($data){
        $groupProductModel = new Groupproduct();
        //查询团购商品
        $groupProduct = $groupProductModel->findGroupProduct($data);
        return $groupProduct;
    }


    /**
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 根据规格获得商品价格
     */
    public function getProductPrice()
    {
        $param = self::getHttpParam();
        if (empty($param->productId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'productId不能这空');
        } else {
            $productModel = new \app\admin\model\Product();
            //团购商品限购数量
            if(isset($param->is_group) && $param->is_group == $productModel::STATUS_VALID){
            $params['storeId'] = $param->storeId;
            $params['productId'] = $param->productId;
            //获取团购限购数量
            $getGroup = $this->getGroupLimitCount($params);
            $limitCount = $getGroup->confineCount;
            }




            $product = $productModel->getProduct($param);
            $price = [];
            if($product->isIntegralPurchase == $productModel::IS_INTEGALPURCHASE_2){
                $getIntegralProduct = $product->getIntegralProduct;
                $price['changeIntegral'] =$getIntegralProduct->changeIntegral;
            }
            if ($product->specClass == 2) {
                $specDetails2 = input('post.specDetails2')?input('post.specDetails2'):NULL;
                $specDetails3 = input('post.specDetails3')?input('post.specDetails3'):NULL;
                $specGroups = Productspecgroups::where(
                    ['productId' => input('post.productId'),
                        'specDetails1' => input('post.specDetails1'),
                        'specDetails2' => $specDetails2,
                        'specDetails3' => $specDetails3])
                    ->find();
                $price['specId'] = $specGroups->id;
                $price['limitCount'] = $specGroups->limitCount;

                if(isset($limitCount)){
                    $price['limitCount'] = $limitCount;
                }

                $price['storeCount'] = $specGroups->storeCount;
                $price['isStore'] = $specGroups->isStore;

                if (!empty($specGroups->price)) {
                    $price['price'] = $specGroups->price;
                }

                if (!empty($specGroups->purchasePrice)) {
                    $price['purchasePrice'] = $specGroups->purchasePrice;
                }

                if (!empty($specGroups->groupPrice)) {
                    $price['groupPrice'] = $specGroups->groupPrice;
                }


                if (!empty($specGroups->currentPrice)) {
                    $price['currentPrice'] = $specGroups->currentPrice;
                }

                if (!empty($specGroups->memberPrice)) {
                    $price['memberPrice'] = $specGroups->memberPrice;
                }

                if(!empty($specGroups->integralPrice)){
                    $price['integralPrice'] = $specGroups->integralPrice; //积分商品价格
                }

                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $price);
            } else {
                $price['limitCount'] = $product->limitCount;

                if(isset($limitCount)){
                    $price['limitCount'] = $limitCount;
                }

                $price['storeCount'] = $product->storeCount;
                $price['isStore'] = $product->isStore;
                if (!empty($product->price)) {
                    $price['price'] = $product->price;
                }
                if (!empty($product->purchasePrice)) {
                    $price['purchasePrice'] = $product->purchasePrice;
                }
                if (!empty($product->currentPrice)) {
                    $price['currentPrice'] = $product->currentPrice;
                }
                if (!empty($product->memberPrice)) {
                    $price['memberPrice'] = $product->memberPrice;
                }

                if (!empty($product->groupPrice)) {
                    $price['groupPrice'] = $product->groupPrice;
                }


                if(!empty($product->integralPrice)){
                    $price['integralPrice'] = $product->integralPrice; //积分商品价格
                }

                return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $price);
            }
        }
    }
}